package com.yueke.gemini.wxpublicapi.accesstoken;


import com.yueke.gemini.wxpublicapi.AccessTokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by admin on 2017/5/15.
 */
public class AccessTokenThread implements Runnable{
    private static final Logger logger = LoggerFactory.getLogger(AccessTokenThread.class);
    private String appID;//应用ID
    private String appSecret; //应用密钥
    public static AccessToken access_token = null;

    public AccessTokenThread(String appID,String appSecret){
        this.appID = appID;
        this.appSecret = appSecret;
    }

    @Override
    public void run() {
        while(true){
            try {
                //调用工具类获取access_token(每日最多获取100000次，每次获取的有效期为7200秒)
                access_token = AccessTokenUtil.getAccessToken(appID, appSecret);
                if(null!=access_token){
                    System.out.println("accessToken获取成功："+access_token.getExpires_in());
                    //7000秒之后重新进行获取
                    Thread.sleep((access_token.getExpires_in()-200)*1000);
                }else{
                    //获取失败时，60秒之后尝试重新获取
                    Thread.sleep(60*1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
